package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrderService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;


public class OrderServiceImpl implements OrderService {
    @Override
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        int orderId = 0;
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        // 开始事物
        try {
            DBUtil.beginTransaction();
            //1. 查询当前用户购物车中当前商家的所有的食品(目的是放入订单明细中)
            Cart cart = new Cart();
            cart.setUserId(userId);
            cart.setBusinessId(businessId);
            List<Cart> cartList = cartDao.listCart(cart);
            //2. 创建订单，并获取到订单的编号
            Orders orders = new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            orders.setDaId(daId);
            orders.setOrderTotal(orderTotal);
            orderId = ordersDao.saveOrders(orders);
            //3. 向订单明细表中批量添加明细的数据
            List<OrderDetailet> orderDetailetList = new ArrayList<>();
            for(Cart c:cartList){
                OrderDetailet od = new OrderDetailet();
                od.setOrderId(orderId);
                od.setFoodId(c.getFoodId());
                od.setQuantity(c.getQuantity());
                orderDetailetList.add(od);
            }
            orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
            //4. 清空购物车(条件：当前用户、当前商家)
            cartDao.removeCart(cart);
            // 提交事物
            DBUtil.commitTransaction();
        } catch (Exception e) {
            orderId = 0;
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
        }
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {
            //1. 根据订单ID查询到订单的信息(多对一：商家)
            orders = ordersDao.getOrdersById(orderId);
            //2. 根据订单的ID查询订单明细的信息
            List<OrderDetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
            orders.setOrderDetailetList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        List<Orders> list = new ArrayList<>();
        try {
            //1. 根据用户的ID查询订单信息(多对一：商家)
            list = ordersDao.listOrdersByUserId(userId);
            //2. 查询多个订单的订单明细信息
            for(Orders o:list) {
                List<OrderDetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setOrderDetailetList(odList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }
}
